//
//  GuideController.m
//  QuickManHR
//
//  Created by Li Huan Zhou on 16/6/20.

//

#import "GuideController.h"
#import "ViewController.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface GuideController ()<UIScrollViewDelegate>

//用来存储所有引导页的数组
@property (nonatomic,strong)NSArray *bgImgsArr;

//用来滚动的scrollview
@property (nonatomic,strong) UIScrollView *scrollView;

//显示页数圆点的pageControl
@property (nonatomic,strong)UIPageControl *pageControl;


@end

@implementation GuideController

// 标记
static NSString * const reuseIdentifier = @"Cell";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    //设置scrollview的contentsize属性,让界面可以滚动
    self.scrollView.contentSize = CGSizeMake(self.bgImgsArr.count * self.scrollView.frame.size.width, 0);
    //取消滚动条
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    //设置分页效果
    self.scrollView.pagingEnabled = YES;
    
    //创建UIImageView
    for (int i = 0; i < self.bgImgsArr.count; i ++) {
        UIImageView *imageView  = [[UIImageView alloc] init];
        CGFloat imageViewX = i * kWidth;
        imageView.frame = CGRectMake(imageViewX, 0,kWidth, self.scrollView.frame.size.height);
        //添加图片
        imageView.image = self.bgImgsArr[i];
        //向scrollview中添加几张图片
        [self.scrollView addSubview:imageView];
    }
   
    //设置pageControl的总个数
    self.pageControl.numberOfPages = self.bgImgsArr.count;
    //设置pagecontrol当前处于第几个
    self.pageControl.currentPage = 0;
 
    
}

#pragma mark - UIScrollViewDelegate

//界面滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger page  = (scrollView.contentOffset.x + scrollView.frame.size.width*0.5)/scrollView.frame.size.width;
    self.pageControl.currentPage = page;
    CGPoint point = self.scrollView.contentOffset;
    //判断point是否处于最后一页
    if (point.x > self.scrollView.frame.size.width * (self.bgImgsArr.count - 1)) {
        //获取应用程序对象
        UIApplication *app = [UIApplication sharedApplication];
        app.keyWindow.rootViewController = [[ViewController alloc] init];
    }else{
        point.x += self.scrollView.frame.size.width;
    }
}


#pragma mark - 懒加载
- (NSArray *)bgImgsArr {
    if (!_bgImgsArr) {
        
        _bgImgsArr = @[
                       [UIImage imageNamed:@"引导页－1"],
                       [UIImage imageNamed:@"引导页－2"],
                       [UIImage imageNamed:@"引导页－3"],
                       [UIImage imageNamed:@"引导页－4"],
                       ];
        
    }
    return _bgImgsArr;
}
@end
